/* INGROUP.C
 * Copyright (c) 2007 MissingNODE 
 * Based on GPL OpenQM Code
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * 
 * START-HISTORY:
 * Oct-10-2007 Initial Revision
 * END-HISTORY
 *
 * START-DESCRIPTION:
 *
 * END-DESCRIPTION
 *
 * START-CODE
 */

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <grp.h>

short int in_group(char * group_name)
{
 short int status = 0;
 int group_id;
 struct group * grp;
 int num_groups;
 gid_t * groups;
 short int i;

 grp = getgrnam(group_name);
 if (grp != NULL)
  {
   group_id = grp->gr_gid;

   num_groups = getgroups (0, NULL);
   groups = (gid_t *) malloc(num_groups * sizeof(gid_t));
   if (groups != NULL)
    {
     if (getgroups(num_groups, groups) >= 0)
      {
       for(i = 0; i < num_groups; i++)
        {
         if (groups[i] == group_id)
          {
           status = 1;
           break;
          }
        }
      }
     free(groups);
    }
  }

 return status;
}

/* END-CODE */
